package com.ssl.sslliving.commodity.controller;

import com.aliyun.oss.*;
import com.aliyun.oss.model.PutObjectRequest;
import com.ssl.common.utils.R;
import com.ssl.sslliving.commodity.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

@RestController
@Slf4j
public class TestController {

    //@RequestMapping("/test")
    //public R testUpload() {
    //    // Endpoint以华东1（杭州）为例，其它Region请按实际情况填写。
    //    String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
    //    // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
    //    //用户登录名称 sslliving-10000@1504703645453442.onaliyun.com
    //    //AccessKey ID LTAI5t8AajY9zXpnDTTALB74
    //    //AccessKey Secret 2JvJTuMuX9moN76BMfKWlhzIGHJpwv
    //    String accessKeyId = "LTAI5t8AajY9zXpnDTTALB74";
    //    String accessKeySecret = "2JvJTuMuX9moN76BMfKWlhzIGHJpwv";
    //    // 填写Bucket名称，例如examplebucket。
    //    String bucketName = "sslliving-10000";
    //    // 填写Object完整路径，完整路径中不能包含Bucket名称，例如exampledir/exampleobject.txt。
    //    String objectName = "C:\\Users\\28181\\Desktop\\hello.txt";
    //
    //    // 创建OSSClient实例。
    //    OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    //
    //    try {
    //        // 填写字符串。
    //        String content = "Hello OSS";
    //
    //        // 创建PutObjectRequest对象。
    //        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, new ByteArrayInputStream(content.getBytes()));
    //
    //        // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
    //        // ObjectMetadata metadata = new ObjectMetadata();
    //        // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
    //        // metadata.setObjectAcl(CannedAccessControlList.Private);
    //        // putObjectRequest.setMetadata(metadata);
    //
    //        // 上传字符串。
    //        ossClient.putObject(putObjectRequest);
    //    } catch (OSSException oe) {
    //        System.out.println("Caught an OSSException, which means your request made it to OSS, "
    //                + "but was rejected with an error response for some reason.");
    //        System.out.println("Error Message:" + oe.getErrorMessage());
    //        System.out.println("Error Code:" + oe.getErrorCode());
    //        System.out.println("Request ID:" + oe.getRequestId());
    //        System.out.println("Host ID:" + oe.getHostId());
    //    } catch (ClientException ce) {
    //        System.out.println("Caught an ClientException, which means the client encountered "
    //                + "a serious internal problem while trying to communicate with OSS, "
    //                + "such as not being able to access the network.");
    //        System.out.println("Error Message:" + ce.getMessage());
    //    } finally {
    //        if (ossClient != null) {
    //            ossClient.shutdown();
    //        }
    //    }
    //    return null;
    //}

    @Resource
    private OSSClient ossClient;

    @Resource
    private CategoryService categoryService;

    @RequestMapping("/test2")
    public R testUpload2() throws FileNotFoundException {
        FileInputStream inputStream = new FileInputStream("C:\\Users\\28181\\Desktop\\hi.txt");
        ossClient.putObject("sslliving-10000", "hi.txt", inputStream);
        ossClient.shutdown();
        return R.ok("上传成功");
    }

    @RequestMapping("test3")
    public R testGetCascadedCategoryId() {
        Long[] cascadedCategoryId = categoryService.getCascadedCategoryId(301L);
        return R.ok().put("data", cascadedCategoryId);
    }


}
